<template>
	<view>
		<!--基础信息-->
		<view @click="pickerSelectFun('跟进方式')" class="flex-white-plr26 ptb20 bdb_f5">
			<text class="mr26">跟进方式</text>
			<view :class="salemode ? '' : 'cBlack'">
				{{salemode ? salemode : '请选择'}}
				<u-icon class="ml26" name="arrow-right" size="40" color="#888888"></u-icon>
			</view>
		</view>
		<view @click="pickerSelectFun('跟进阶段')" class="flex-white-plr26 ptb20 bdb_f5">
			<text class="mr26">跟进阶段</text>
			<view :class="salestage ? '' : 'cBlack'">
				{{salestage ? salestage : '请选择'}}
				<u-icon class="ml26" name="arrow-right" size="40" color="#888888"></u-icon>
			</view>
		</view>
		<!-- 弹出出选择 -->
		
		<u-select v-model="selectShow" :list="selectList" @confirm="selectConfirmFun"></u-select>
		<u-picker :params="pickerTimeParams" :safe-area-inset-bottom="true" end-year="2100" v-model="DateShow" @confirm="pickerTimeConfirmFun" :default-time="defaulTime" mode="time"></u-picker>
		
		<view class="flex-white-plr26-column ptb20 mt32 bdb_f5">
			<view style="margin-bottom: 8rpx;">
				<text>跟进内容</text>
				<text class="redXingh">*</text>
			</view>
			<u-input v-model="link_body" type="textarea" :border="true" :height="100" :auto-height="true" />
		</view>
		<view class="flex-white-plr26 ptb20 bdb_f5" @click="DateShow = true, pickerDateType = 'link_time'">
			<text class="mr26">跟进时间</text>
			<view class="w420">{{$u.timeFormat(link_time, 'yyyy-mm-dd hh:MM:ss')}}</view>
			<u-icon class="ml26" name="calendar" size="40" color="#888888"></u-icon>
		</view>
		<view class="flex-white-plr26 ptb20 bdb_f5" @click="DateShow = true, pickerDateType = 'next_time'">
			<text class="mr26">下次跟进</text>
			<view class="w420">
				<text v-if="next_time">{{$u.timeFormat(next_time, 'yyyy-mm-dd hh:MM:ss')}}</text>
				<text class="cBlack" v-else>请选择下次跟进时间</text>
			</view>
			<u-icon class="ml26" name="calendar" size="40" color="#888888"></u-icon>
		</view>
		
		<!--提交按钮-->
		<view class="submitView">
			<u-button type="primary" class="submitBtn" :ripple="true" ripple-bg-color="#909399" @click="submitBtn">
				{{pageType =='' ? '提交' : '保存'}}</u-button>
		</view>
	</view>
</template>

<script>
	let that = '';
	import {
		apiCstDict,
		apiClue
	} from '@/api/crm.js'
	import { getNowDate } from '@/utils/date.js'
	export default {
		data() {
			return {
				name: '',
				link_body: '',
				link_time:'',
				next_time:'',
				salemode:'网上联系',
				salestage: '初步意向',
				pickerParams: {},
				pickerShow: false,
				pickerMode: 'time',
				selectShow: false,
				selectList: [],//下拉选择数据
				selectType: '',
				clue_name:'',
				clue_id:'',
				owner_user_id: uni.$userInfo.id,//归属员工id
				sys_user_id:uni.$userInfo.id,
				DateShow: false,
				pickerDateType:'',
				pickerTimeParams: { year: true, month: true, day: true, hour: true, minute: true, timestamp: true },
				pageType: '',//当前页操作类型，add/update
				xgInfoObj: {},
				isAdd: 0,
				fkId: '',
				zhuanWeiClientId: '',
				cstDict:''//所有字典
			}
		},
		onLoad(e) {
			that = this;
			
			let dateObj = getNowDate(true);
			that.defaulTime = dateObj.nowDate;
			that.link_time = dateObj.st;
			
			if(e.clue_name && e.clue_name != 'undefined') {
				that.clue_name = e.clue_name;
			}
			if(e.clue_id && e.clue_id != 'undefined') {
				that.clue_id = e.clue_id;
			}
			
			if (e.type == 'update' || e.type == 'zhuanClient') {
				that.setDataFun();
				that.pageType = e.type;
				if (e.type == 'update') {
					uni.setNavigationBarTitle({
						title: '修改客户信息'
					})
				} else {
					uni.setNavigationBarTitle({
						title: '线索转为客户'
					})
				}
			}
			
			//载入字典
			let reqData = {
				action: 'get_list',
				params: {
					
				}
			}
			apiCstDict(reqData)
				.then(res => {
					let dictArr = res.data;
					let newArr=[];
					for(var key in dictArr){
						let rowData=[];
						for (var i = 0; i < dictArr[key].length; i++) {
							rowData.push({
								label: dictArr[key][i].name,
								value:dictArr[key][i].id
							})
						}
						newArr[key]=rowData;
					}
					that.cstDict=newArr;
				});

			if (e.isAdd) {
				that.isAdd = e.isAdd;
				if (uni.$addInfo) {
					that.linkman = uni.$addInfo.linkman;
					that.mobile = uni.$addInfo.mobile;
					that.name = uni.$addInfo.name;
					that.qicq = uni.$addInfo.qicq || '';
					that.address = uni.$addInfo.address || '';
					that.salemode = uni.$addInfo.salemode || '';
					that.weixin = uni.$addInfo.weixin || '';
					if (that.region) {
						let regionArr = (that.region).split(" ");
						that.province = regionArr[0];
						that.city = regionArr[1];
						that.area = regionArr[2];
					}
					if (uni.$addInfo.fkId) {
						that.fkId = uni.$addInfo.fkId;
					}
				}
			}
		},
		methods: {
			// 单选框
			radioGroupChange(e) {
				that.gender = e;
			},
			//下拉选择框架
			pickerSelectFun: function(str) {
				if (str == '跟进阶段') {
					that.selectList = that.cstDict.salestage;
				} else if (str == '跟进方式') {
					that.selectList = that.cstDict.salemode
				}
				that.selectShow = true;
				that.selectType = str;
			},
			//下拉选择点击确定之后处理
			selectConfirmFun: function(e) {
				let str = e[0].label;
				let val = e[0].value;
				if (that.selectType == '跟进阶段') {
					that.salestage = str;
				} else if (that.selectType == '跟进方式') {
					that.salemode = str;
				}
			},
			// 时间选择器点击确定按钮，返回当前选择的值
			pickerTimeConfirmFun: function(e) {
				let gjTime = parseInt(e.timestamp + '000');
				let dateTime = new Date().getTime();
				if(that.pickerDateType == 'link_time') {
					if(gjTime > dateTime) {
						uni.$myModalFun('新建跟进记录的跟进时间，不能大于当前时间，请修改！')
						return
					}
					that.link_time =  e.year + '-' + e.month + '-' + e.day + ' ' + e.hour + ':' + e.minute;
				} else {
					if(gjTime < dateTime) {
						uni.$myModalFun('下次跟进记录的跟进时间，不能小于当前时间，请修改！')
						return
					}
					that.next_time =e.year + '-' + e.month + '-' + e.day + ' ' + e.hour + ':' + e.minute;;
				}
			},
			// 提交按钮
			submitBtn: function() {
				
				if (that.link_body == '') {
					uni.showModal({
						title: '提示',
						content: '跟进的内容不能为空，请输入！',
						showCancel: false
					})
					return
				}
				
				//请求参数
				let reqObj = {
					req: {
						clue_id: that.clue_id,
						link_time:that.link_time,
						next_time: that.next_time,
						salemode: that.salemode,
						salestage: that.salestage,
						link_body: that.link_body,
						create_user_id: that.owner_user_id
					}
				}
				
				let action = 'clue_trace_add';
				
				if (that.pageType == 'update') {
					
					action = 'clue_trace_update'
					
					reqObj.req.id = that.xgInfoObj.id;
					uni.showLoading({
						title: '修改中...'
					})
					
				}else {
					uni.showLoading({
						title: '提交中...'
					})
				}
				
				let reqData = {
					action: action,
					params: reqObj
				}
				
				apiClue(reqData)
					.then(res => {
						console.log('返回结果');
						console.log(res);
						
						if (action == 'update') {
							uni.showToast({
								title: '修改成功',
								icon: 'none',
								duration: 888,
								mask: true
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 888)
						} else {
							uni.showToast({
								title: '新增成功',
								icon: 'none',
								duration: 1000,
								mask: true
							})
							let addObj = reqObj.req;
							addObj.id = res.data;
							uni.$emit('addGjItemInListFun')
							setTimeout(() => {
								uni.redirectTo({
									url: './trace_detail?index=0&id='+res.data
								})
							}, 1000)
						}
					})
			},
			// 初始化修改数据
			setDataFun: function() {
				let obj = uni.$gjInfo;
				that.xgInfoObj = uni.$gjInfo;
				that.name = obj.name;
				that.mobile = obj.mobile;
				that.address = obj.address;
				// that.customerstatus = obj.customerstatus;
				that.salestage = obj.salestage;
				that.owner_user_id = obj.owner_user_id;
				that.linkman = obj.linkman;
				that.type = obj.type;
				that.ecotype = obj.ecotype;
				that.level = obj.level;
				that.industry = obj.industry;
				that.stars = obj.stars;
				that.salemode = obj.salemode;
				that.link_body = obj.link_body;

			}
		}
	}
</script>

<style>
	page {
		background-color: #F8F8F8;
		padding-bottom: 260rpx;
	}

	.u-radio {
		width: 200rpx !important;
	}
</style>
